package com.java.service;

import com.java.dao.UserMapper;
import com.java.pojo.EnterGood;
import com.java.pojo.Page;
import com.java.pojo.Stores;
import com.java.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<User> getPageByUser(Integer a, String username) {
        int b=4;//显示条数
        Page<User> page=new Page<>();
        page.setCurryPage(a);//设置当前页码
        page.setRows(b);//设置每页显示的条数
        Integer count = userMapper.getCountUser(username);//总条数
        page.setTotalCount(count);//总条数
        int start=(a-1)*b;/*开始的索引*/
        List<User> list = userMapper.getListUser(start, b, username);
        page.setList(list);/*赋值list*/
        int totalPae=count % b==0 ? count / b : (count / b)+1;
        page.setTotalPae(totalPae);/*设置总页数*/
        if(page==null){
            page=null;
        }
        return page;
    }

    @Override
    public Integer getCount(String username) {
        return userMapper.getCountUser(username);
    }

    @Override
    public int addUser(User user) {
        User user1 = userMapper.getUserByName(user.getUsername());
        if(user1==null){
        return userMapper.addUser(user);
        }else {
            return 0;
        }

    }

    @Override
    public int delUser(Integer id) {
        return userMapper.delUser(id);
    }

    @Override
    public User getUserByName(String username) {
        return userMapper.getUserByName(username);
    }

    @Override
    public User getUserByNameAndPwAndSid(User user) {
        return userMapper.getUserByNameAndPwAndSid(user);
    }


}
